package helperClasses;

/**
 * 
 * @author Felix Schliephacke
 * 
 */
public class Help {

	public static int[] calculateColumnIndices(final int squareIndex,
			final int squareSize) throws IllegalArgumentException {
		if (squareIndex < 0) {
			throw new IllegalArgumentException(
					"Square index must be greater than or equal to 0.");
		}
		if (squareSize <= 0) {
			throw new IllegalArgumentException("Size must be greater than 0.");
		}
		final int size = squareSize * squareSize;
		if (squareIndex >= size) {
			throw new IllegalArgumentException(
					"Square index must be lower than " + size + ".");
		}
		final int[] columnIndices = new int[squareSize];
		final int offset = squareIndex % squareSize * squareSize;
		for (int i = 0; i < squareSize; i++) {
			columnIndices[i] = i + offset;
		}
		return columnIndices;
	}

	public static int[] calculateRowIndices(final int squareIndex,
			final int squareSize) throws IllegalArgumentException {
		if (squareIndex < 0) {
			throw new IllegalArgumentException(
					"Square index must be greater than or equal to 0.");
		}
		if (squareSize <= 0) {
			throw new IllegalArgumentException("Size must be greater than 0.");
		}
		final int size = squareSize * squareSize;
		if (squareIndex >= size) {
			throw new IllegalArgumentException(
					"Square index must be lower than " + size + " but was "
							+ squareIndex + ".");
		}
		final int[] rowIndices = new int[squareSize];
		final int offset = squareIndex / squareSize * squareSize;
		for (int i = 0; i < squareSize; i++) {
			rowIndices[i] = i + offset;
		}
		return rowIndices;
	}

	public static int calculateSquareIndex(final int row, final int column,
			final int squareSize) {
		if (row < 0) {
			throw new IllegalArgumentException(
					"Row must be greater than or equal to 0 but was: " + row
							+ ".");
		}
		if (column < 0) {
			throw new IllegalArgumentException(
					"Column must be greater than or equal to 0 but was: "
							+ column + ".");
		}
		if (squareSize < 1) {
			throw new IllegalArgumentException(
					"Square size must be greater than 0 but was: " + squareSize
							+ ".");
		}
		final int size = squareSize * squareSize;
		if (row >= size) {
			throw new IllegalArgumentException("Row index out of bounds: "
					+ row);
		}
		if (column >= size) {
			throw new IllegalArgumentException("Column index out of bounds: "
					+ column);
		}
		final int rowOffset = row / squareSize * squareSize;
		final int columnOffset = column / squareSize;
		return rowOffset + columnOffset;
	}

	public static int countObjectOccurencesInArray(final Object object,
			final Object[] array) {
		int count = 0;
		for (final Object temp : array) {
			if (temp.equals(object)) {
				count++;
			}
		}
		return count;
	}

	public static boolean isPositiveSquare(final int arg) {
		if (arg <= 0) {
			return false;
		}
		for (int i = 1; i <= (int) Math.sqrt(arg); i++) {
			if (arg == i * i) {
				return true;
			}
		}
		return false;
	}
}
